<template>
    <div class="tags-wrap" v-if="showTags">
        <scroll-pane ref="scrollPane">
            <div class="tags-li" v-for="(item, index) in tagsList" :class="{ active: isActive(item.path) }" :key="index">
                <router-link :to="item.path" ref="tag" class="tags-li-title">{{ item.title }}</router-link>
                <span v-if='noTagRouter && noTagRouter!= item.path' class="tags-li-icon" @click="closeTags(index)"><i class="el-icon-close"></i></span>
            </div>
        </scroll-pane>
        
        <div class="tags-close-box">
            <el-dropdown @command="handleTags">
                <el-button size="mini" type="primary">
                    标签选项
                    <i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu size="small" slot="dropdown">
                    <el-dropdown-item command="other">关闭其他</el-dropdown-item>
                    <el-dropdown-item command="all">关闭所有</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
	import PermissionMapper from '@/configs/permission-mapper';
    import ScrollPane from './ScrollPane'
    import { pms2MenuList,decrypt} from '@/filters/filter'
    import bus from '@/jslibs/bus';
	import Utils from '@/jslibs/utils'
export default {
    data() {
        return {
            tagsList: [],
            defaultTag:{},
            menuList:[],
            noTagRouter:""
        };
    },
    
    computed: {
        showTags() {
            return this.tagsList.length > 0;
        }
    },
    
    watch: {
        $route(newValue, oldValue) {
            this.moveToCurrentTag();
            this.setTags(newValue);
        }
    },
    
    created() {
        // const encryptedPermission = sessionStorage.getItem('permission')
        // // 解码并转换成对象格式
        // this.permissionList = JSON.parse(decrypt(encryptedPermission));
        // this.menuList = pms2MenuList(this.permissionList);
		 const role = Utils.getSession('role');
		 const schoolArr = JSON.parse(JSON.stringify(PermissionMapper));
		 if(role != 0){
		 	schoolArr.shift()
		 }
		 this.menuList = schoolArr;
        if(this.menuList && this.menuList.length && this.menuList[0].children && this.menuList[0].children.length){
            this.noTagRouter =  this.menuList[0].children[0].route;
            //默认每次都会展示登录之后显示的第一个页面，并且该页面不能关闭
            this.defaultTag = {
                title: this.menuList[0].children[0].name,
                path: this.menuList[0].children[0].route,
                name: this.menuList[0].children[0].name
            }
            this.tagsList.push(this.defaultTag)
        }
        this.setTags(this.$route);
    },
    
    methods: {
        isActive(path) {
            return path === this.$route.path;
        },
        
        // 关闭单个标签
        closeTags(index) {
            const delItem = this.tagsList.splice(index, 1)[0];
            const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1];
            if (item) {
                delItem.path === this.$route.path && this.$router.push(item.path);
            } else {
                this.$router.push('/user-list');
            }
        },
        
        // 关闭全部标签
        closeAll() {
            if(this.tagsList.length > 1){//当只有默认页面时，不允许关闭所有
                this.tagsList = [];
                this.$router.push('/user-list');
            }
        },
        
        // 关闭其他标签
        closeOther() {
            const curItem = this.tagsList.filter(item => {
                return item.path === this.$route.path;
            });
            this.tagsList = curItem;
            this.tagsList.unshift(this.defaultTag);
        },
        
        // 设置标签
        setTags(route) {
            const isExist = this.tagsList.some(item => {
                return item.path === route.path;
            });
            if (!isExist) {
                const index = route.meta.length - 1; // 选取最后一个最为标签
                if (index) {
                    this.tagsList.push({
                        title: route.meta[index].name,
                        path: route.path,
                        name: route.matched[1].components.default.name
                    });
                }
            }
        },

        moveToCurrentTag() {
            const tags = this.$refs.tag;
            this.$nextTick(() => {
                for (const tag of tags) {
                  if (tag.to === this.$route.path) {
                    this.$refs.scrollPane.moveToTarget(tag);
                    if (tag.to !== this.$route.path) {
                        const index = this.$route.meta.length - 1; // 选取最后一个最为标签
                        this.tagsList.push({
                            title: this.$route.meta[index].name,
                            path: this.$route.path,
                            name: this.$route.meta[index].name
                        });
                    }
                    break;
                  }
                }
            });
        },
        
        handleTags(command) {
            command === 'other' ? this.closeOther() : this.closeAll();
        }
    },
    
    components:{
        ScrollPane
    }
};
</script>

<style lang="less" scoped>
.tags-wrap {
    position: relative;
    height: 40px;
    padding: 5px 0;
    background: #fff;
    padding-right: 120px;
    padding-left: 18px;
    box-shadow: 0 5px 10px #ddd;
	z-index: 9;
}

.tags-li {
    display: inline-block;
    margin: 3px 5px 2px 3px;
    border-radius: 3px;
    font-size: 12px;
    overflow: hidden;
    cursor: pointer;
    height: 30px;
    line-height: 30px;
    border: 1px solid #e9eaec;
    background: #fff;
    padding: 0 5px 0 12px;
    vertical-align: middle;
    color: #666;
    -webkit-transition: all 0.3s ease-in;
    -moz-transition: all 0.3s ease-in;
    transition: all 0.3s ease-in;
}

.tags-li:not(.active):hover {
    background: #f8f8f8;
}

.tags-li.active {
    color: #fff;
    border: 1px solid #FF8A00;
    background-color: #FF8A00;
}

.tags-li-title {
    float: left;
    max-width: 80px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-right: 5px;
    color: #666;
}

.tags-li.active .tags-li-title {
    color: #fff;
}

.tags-close-box {
    position: absolute;
    right: 0;
    top: 15%;
    box-sizing: border-box;
    padding-top: 1px;
    text-align: center;
    width: 110px;
    height: 40px;
    background: #fff;
    z-index: 10;
}
.tags-close-box button {
    height: 32px;
}
</style>
